import { Component, OnInit, ViewChild } from '@angular/core';
import { DataService } from '../../services/data.service';
import { ContentComponent } from '../content/content.component';
import { RequestService } from '../../services/request.service';
// 万用表服务
import { MultimeterService } from '../../components/multimeter/multimeter.service'

// 使用jq
declare var $: any;

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss'],
})
export class HomeComponent implements OnInit {
  @ViewChild('contentchild') content:any;
  @ViewChild('header') header:any;

  // 总页数
  total:number;
  // 当前页数
  current:number;
  // 是否可以翻页
  ispage:boolean;

  // 封面的显示与隐藏
  isvisibel:boolean= true;
  // 目录显示与隐藏
  flag= true;

  constructor(
    public data: DataService,
    public request: RequestService,
    public multimeterService: MultimeterService
  ) { }

  ngOnInit() {
    // $.xcj.scorm.api.loadPage();
    this.total= this.data.content.length-1;
    this.current= this.data.currentPage || 1;

    /**
     * 
     * 测试Rxjs
     * 
     */
    // 同步
     let data=this.request.getData();
     console.log(data);
    //  异步--回调  
    this.request.getCallbackData((data) => {
      console.log(data);
    });

    // 异步--promise
    let promiseData=this.request.getPromiseData();
    promiseData.then((data) => {
      console.log(data)
    })

    // 异步--rxjs
    // let rxjsData=this.request.getRxjsData();
    // rxjsData.subscribe((data) => {
    //   console.log(data)
    // })

    // 过一秒以后撤回操作
    let streem=this.request.getRxjsData();
    var d=streem.subscribe((data) => {
      console.log(data)
    })

    setTimeout(() => {
      d.unsubscribe()
    }, 1000)

    // 多次执行
    // let streemInterval=this.request.getRxjsIntervalData();
    // streemInterval.subscribe((data) => {
    //   console.log(data)
    // })
   
  }


  // 进入课程
  StartPlay() {
    // $.xcj.scorm.api.loadPage();
    





    this.isvisibel = false;
    setTimeout(() => {
      this.DataInit();
    },100)

    if ($.xcj.scorm.api.getComplete() !== "completed") {
      $.xcj.scorm.api.setComplete("incomplete");
      $.xcj.scorm.api.doCommit();
    }
  }



  // 下一页
  NextPage() {
    this.changeCatalogStat()
    this.ispage = this.content.IsPaging();
    console.log(this.ispage)
    if(this.ispage){
      if(this.current < this.total){
        // this.tab.run(this.current);
        this.current++;
        this.data.currentPage= this.current;
        if (this.current == 11) {
          $.xcj.scorm.api.setComplete("completed");
          $.xcj.scorm.api.doCommit();
        }
      }
    }
  }

  // 上一页
  PrevPage() {
    if(this.current>1){
      // this.tab.run1(this.current)
      this.current--;
    }
    this.DataInit();
    // console.log(this.data.content[this.current].hostHalo)
  }


  // 翻页初始化
  DataInit() {
    if(this.data.content[this.current].hostHalo){
      this.data.content[this.current].hostHalo.map((i, j) => {
        this.data.content[this.current].hostHalo[j] = "halo";
      });
    }
  }

  // 打开目录
  showmenu(event) {
    if (event && event.stopPropagation) {
      // 针对 Mozilla 和 Opera
      event.stopPropagation();
    } else if (window.event) {
      // 针对 IE
      window.event.cancelBubble = true;
    }
    this.flag = !this.flag;
  }

  // 目录
  isChildNavShow(item) {
    // console.log(item, "1111");
    if(!item.isGetInfo) return;
    if (JSON.stringify(item.isChildNav) !== "[]") {
      item.isExistence = !item.isExistence;
      return;
    } else {
      this.current = item.currentPage;
    }
  }

  changeCatalogStat() {
    this.data.Catalog.map((item, index) => {
      if(item.currentPage == this.current){
        item.isGetInfo = true;
      }
      if(JSON.stringify(item.isChildNav) !=="[]"){
        if(item.currentPage == this.current && item.studyState == '1'){
          item.studyState = "2";
        }
        item.isChildNav.map((i,j) => {
          if(i.currentPage == this.current){
            i.isGetInfo = true;
          }
          if (this.current > Number(item.maxPage) && i.studyState === "2") {
            item.studyState = "3";
          }
          if (
            i.currentPage < this.current &&
            JSON.stringify(i.isChildNav) == "[]" &&
            i.studyState === "2"
          ) {
            i.studyState = "3";
          }
          if (i.currentPage == this.current && i.studyState === "1") {
            i.studyState = "2";
          }
          
          if(JSON.stringify(i.isChildNav) !=="[]"){
            if (i.currentPage == this.current && i.studyState === "1") {
              i.studyState = "2";
            }
            i.isChildNav.forEach((k,l) =>{
              if (k.currentPage == this.current) {
                k.isGetInfo = true;
              }
              if (this.current > Number(i.maxPage) && k.studyState === "2") {
                i.studyState = "3";
              }
              if (k.currentPage < this.current && k.studyState === "2") {
                k.studyState = "3";
              }
              if (k.currentPage == this.current && k.studyState === "1") {
                k.studyState = "2";
              }
              
            })
          }else{
            if (i.currentPage < this.current && i.studyState === "2") {
              i.studyState = "3";
            }
            if (i.currentPage == this.current && i.studyState === "1") {
              i.studyState = "2";
            }
          }

        })
      }else{
        if (item.currentPage < this.current && item.studyState === "2") {
          item.studyState = "3";
        }
        if (item.currentPage == this.current && item.studyState === "1") {
          item.studyState = "2";
        }
      }
    })
  }



  /**
   * 
   * 测试Rxjs
   * 
   * 
   */


}
